package cn.gov.service;


import cn.gov.dao.IPersonRoleDAO;
import cn.gov.dao.IPersonalServiceStationDAO;
import cn.gov.dao.IRoleDAO;
import cn.gov.vo.PersonRole;
import cn.gov.vo.PersonalServiceStation;
import cn.geracomium.util.service.abs.AbstractService;
import cn.geracomium.util.web.Page;
import cn.geracomium.util.web.StringKit;
import cn.gov.dao.*;
import cn.gov.vo.Jurisdiction;
import cn.gov.vo.PersonRole;
import cn.gov.vo.Role;
import cn.gov.vo.RoleAction;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

@Service
public class AuthServiceImpl extends AbstractService implements IAuthService {

    @Autowired
    private IPersonalServiceStationDAO personalServiceStationDAO ;
    @Autowired
    private IPersonRoleDAO personRoleDAO ;
    @Autowired
    private IRoleDAO roleDAO;

    @Autowired
    private IJurisdictionDAO jurisdictionDAO;

    @Autowired
    private ISuperviseDAO superviseDAO;

    @Autowired
    private IRoleActionDAO roleActionDAO;

    @Override
    public List<Role> list() {
        return roleDAO.findAll();
    }

    @Override
    public boolean addPersonStationAndRole(PersonalServiceStation personalServiceStation, PersonRole personRole) {


        return personalServiceStationDAO.doCreate(personalServiceStation) && personRoleDAO.doCreate(personRole);
    }

    @Override
    public Integer delRole(int roleId) {
        Integer flag = 0;
        if (personRoleDAO.findByRoleId(roleId) == null) {
            flag = roleDAO.delRole(roleId);
        }else{
            flag = 2;
        }
        return flag;
    }

    @Override
    public Page<Role> roleList(Map<String, Object> conditionMap, int pageNumber) {
        Map<String, Object> result = new HashMap<>();
        if (StringKit.notBlank((String) conditionMap.get("roleName"))) {
            result.put("roleName", conditionMap.get("roleName"));
        }
        result.put("indexOf", ((pageNumber > 1) ? PAGE_SIZE * (pageNumber - 1) : 0));
        result.put("pageSize", PAGE_SIZE);
        List<Role> list = this.roleDAO.findSplit(result);
        Long allCount = this.roleDAO.getAllCount(result);
        return new Page<Role>(list, pageNumber, PAGE_SIZE, allCount);
    }

    @Override
    public Map<String, Object> listByPersonalRole(Long accountId) {
        HashMap<String, Object> map = new HashMap<>();
        Set<Integer> roles = new HashSet<>();
        Set<Role> roleList = null;
        Set<String> optionIds = new HashSet<>();
        Set<String> actions = null;
        Set<Jurisdiction> jurisdictionList = null;
        Set<String> superviseIdList = new HashSet<>();
        Set<PersonRole> personRoleSet = personRoleDAO.findByPersonRole(accountId);
        if (personRoleSet != null || personRoleSet.size() != 0) {
            for (PersonRole personRole : personRoleSet) {
                roles.add(personRole.getRoleId());
            }
            roleList = roleDAO.getOptionIdList(roles.toArray());
            map.put("roleList", roleList);
            for (Role role : roleList) {
                optionIds.add(role.getOptionId());
            }
            map.put("options", optionIds);
            actions = roleActionDAO.getActionIdList(optionIds.toArray());
            map.put("actions", actions);
            jurisdictionList = jurisdictionDAO.getActionList(actions.toArray());
            map.put("jurisdictionList", jurisdictionList);
            for (Jurisdiction jurisdiction : jurisdictionList) {
                superviseIdList.add(jurisdiction.getSuperviseId());
            }
            map.put("supervises",superviseIdList);
            map.put("superviseList", superviseDAO.getSuperviseList(superviseIdList.toArray()));
        }
        return map;
    }

    @Override
    public Boolean addRole(Role role, List<RoleAction> roleActionList) {
        boolean flag = false;
        System.err.println(roleActionList);
        System.err.println(roleActionList);
        System.err.println(roleActionList);
        System.err.println(roleActionList);
        if (role != null || roleActionList.size() != 0) {
            if (this.roleDAO.getRoleName(role.getRoleName()) == null) {
                if (this.roleDAO.saveRole(role)) {
                    Integer count = this.roleActionDAO.batchSave(roleActionList);
                    flag = count == roleActionList.size();
                }
            }
        }
        return flag;
    }
}
